package com.handtransaction.controller;

import com.handtransaction.entity.User;
import com.handtransaction.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author YAO
 * @create 2021-03-24 19:00
 */

/**
 * 处理用户登录控制器
 */
@Controller
public class CheckUserController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "tologin", method = RequestMethod.GET)
    public ModelAndView tologin(HttpServletRequest request, HttpServletResponse response, User user, ModelAndView modelAndView) throws IOException, SQLException {
        response.setContentType("text/html;charset=utf-8");
        String value1 = "", value2 = "";
        Cookie cookie = null;
        Cookie[] cookies = request.getCookies();
        User loginUser = new User();
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                cookie = cookies[i];
                if (cookie.getName().equals("uname")) value1 = cookie.getValue();
                if (cookie.getName().equals("upassword")) value2 = cookie.getValue();
                loginUser = userService.loginUser(new User(null, value2, value1, null, null, null, null,user.getUimg_path()));
            }

                if (loginUser != null) {
                    if (value1.equals(loginUser.getUname()) && value2.equals(loginUser.getUpassword())) {
                        request.getSession().setAttribute("user", loginUser);
                        System.out.println(loginUser.getUimg_path());
                        request.getSession().setAttribute("uimg_path",loginUser.getUimg_path());
                        modelAndView.setViewName("redirect:/ShowALLGoods");
                        return modelAndView;
                    }
                } else {
                    modelAndView.setViewName("user/login");
                    return modelAndView;
                }
            } else {
                modelAndView.setViewName("user/login");
            }
            return modelAndView;
        }

        @RequestMapping(value = "checkUser", method = RequestMethod.POST)
        public String checkUser (HttpServletRequest request, HttpServletResponse response, Model model)throws IOException {
            HttpSession session = request.getSession();
            response.setContentType("text/html;charset=utf-8");
            String uname = request.getParameter("uname");
            String upassword = request.getParameter("upassword");
            if(uname.equals("admin")&&upassword.equals("123456")){
                return "redirect:/index.jsp";
            }
            User loginUser = userService.loginUser(new User(null, upassword, uname, null, null, null, null));
            if (loginUser == null) {
                request.setAttribute("msg", "用户名或密码错误");
                request.setAttribute(uname, "uname");
                return "user/login";
            }
            if(loginUser.getUpid() == 2){ //upid = 2 = 冻结状态，不可登录
                request.setAttribute("msg","该用户已被冻结，不可登录");
                return "user/login";
            }
            else {
                request.getSession().setAttribute("user", loginUser);
                request.getSession().setAttribute("uimg_path",loginUser.getUimg_path());
                //			如果用户选中了自动登录复选框，向浏览器发送两个Cookie
                if (request.getParameter("check") != null && (request.getParameter("check").equals("check"))) {
                    Cookie namecookie = new Cookie("uname", uname);
                    Cookie passcookie = new Cookie("upassword", upassword);
                    namecookie.setMaxAge(60 * 60 * 24 * 10);
                    passcookie.setMaxAge(60 * 60 * 24 * 10);
                    response.addCookie(namecookie);
                    response.addCookie(passcookie);
                }

                return "redirect:/ShowALLGoods";
            }
        }
    }


